﻿' LibWowArmory
' by Ronald M. Clifford (roncli@roncli.com)
'
' This source code is released under the GNU Library General Public License (LGPL) Version 2.1.

Imports Newtonsoft.Json
Imports System
Imports System.Collections.Generic
Imports System.Text.RegularExpressions

Namespace roncliProductions.LibWowArmory

    ''' <summary>
    ''' A class that defines a calendar event
    ''' </summary>
    ''' <remarks></remarks>
    Public Class CalendarDetail
        Inherits WowArmoryData

        Private cdDetail As New CalendarDetailSchema.calendarDetail
        Private iInvitees As New List(Of Invitee)
        Private strRealm As String
        Private strName As String
        Private intID As Integer

        ''' <summary>
        ''' The realm of the character for the calendar to lookup
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property Realm() As String
            Get
                Return strRealm
            End Get
            Set(ByVal value As String)
                strRealm = value
            End Set
        End Property

        ''' <summary>
        ''' The name of the character for the calendar to lookup
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property Name() As String
            Get
                Return strName
            End Get
            Set(ByVal value As String)
                strName = value
            End Set
        End Property

        ''' <summary>
        ''' The identification number of the event
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property ID() As Integer
            Get
                Return intID
            End Get
            Set(ByVal value As Integer)
                intID = value
            End Set
        End Property

        ''' <summary>
        ''' The URL that will be used to retrieve the event detail from the armory
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overrides ReadOnly Property URL() As String
            Get
                Return String.Format("http://www.wowarmory.com/vault/calendar/detail.json?callback=calendar.loadEventDetail&r={0}&n={1}&e={2}", strRealm, strName, intID)
            End Get
        End Property

        ''' <summary>
        ''' Indicates to the inherited class that this request requires login
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overrides ReadOnly Property RequiresLogin() As Boolean
            Get
                Return True
            End Get
        End Property

        ''' <summary>
        ''' The summary of the event
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Summary() As String
            Get
                Return cdDetail.summary
            End Get
        End Property

        ''' <summary>
        ''' The event's calendar type
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property CalendarType() As String
            Get
                Return cdDetail.calendarType
            End Get
        End Property

        ''' <summary>
        ''' The date of the event, in UTC
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property UTCStart() As Date
            Get
                Return New Date(1970, 1, 1).AddMilliseconds(cdDetail.start)
            End Get
        End Property

        ''' <summary>
        ''' The type of event
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Type() As String
            Get
                Return cdDetail.type
            End Get
        End Property

        ''' <summary>
        ''' The owner of the event
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Owner() As String
            Get
                Return cdDetail.owner
            End Get
        End Property

        ''' <summary>
        ''' Indicates whether the event is locked or not
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Locked() As Boolean
            Get
                Return cdDetail.locked
            End Get
        End Property

        ''' <summary>
        ''' The timezone offset of the event's server
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property TimezoneOffset() As TimeSpan
            Get
                Return New TimeSpan(0, 0, 0, 0, CInt(cdDetail.tz))
            End Get
        End Property

        ''' <summary>
        ''' The date of the event, in local server time
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Start() As DateTime
            Get
                Return New Date(1970, 1, 1).AddMilliseconds(cdDetail.start).AddMilliseconds(cdDetail.tz)
            End Get
        End Property

        ''' <summary>
        ''' The icon of the event
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Icon() As String
            Get
                Return cdDetail.icon
            End Get
        End Property

        ''' <summary>
        ''' The description of the event
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Description() As String
            Get
                Return cdDetail.description
            End Get
        End Property

        ''' <summary>
        ''' The location of the event
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Location() As String
            Get
                Return cdDetail.location
            End Get
        End Property

        ''' <summary>
        ''' Determines if this toon is the moderator of this event
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Moderator() As Boolean
            Get
                Return cdDetail.moderator
            End Get
        End Property

        ''' <summary>
        ''' The list of invitees for the event
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property Invitees() As List(Of Invitee)
            Get
                Return iInvitees
            End Get
            Set(ByVal value As List(Of Invitee))
                iInvitees = value
            End Set
        End Property

        ''' <summary>
        ''' A constructor to create new event information
        ''' </summary>
        ''' <param name="strUsername">The World of Warcraft/battle.net account username</param>
        ''' <param name="strPassword">The World of Warcraft/battle.net account password</param>
        ''' <param name="strRealm">The realm of the character for the calendar to lookup</param>
        ''' <param name="strName">The name of the character for the calendar to lookup</param>
        ''' <param name="intID">The identification number of the event</param>
        ''' <remarks></remarks>
        Public Sub New(ByVal strUsername As String, ByVal strPassword As String, ByVal strRealm As String, ByVal strName As String, ByVal intID As Integer)
            Me.Username = strUsername
            Me.Password = strPassword
            Me.strRealm = strRealm
            Me.strName = strName
            Me.intID = intID
            Load()
        End Sub

        ''' <summary>
        ''' Loads and deserialzes the JSON data
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub Load()
            MyBase.Retrieve()

            strData = Regex.Match(strData, "calendar.loadEventDetail\((.*)\);").Groups(1).Value

            cdDetail = CType(JavaScriptConvert.DeserializeObject(strData, GetType(CalendarDetailSchema.calendarDetail)), CalendarDetailSchema.calendarDetail)

            iInvitees.Clear()

            If cdDetail Is Nothing Then
                Throw New ArgumentException("The event ID did not return any information.", "intID")
                Exit Sub
            End If

            If cdDetail.invites Is Nothing Then
                Exit Sub
            End If

            For Each iInvite As CalendarDetailSchema.invite In cdDetail.invites
                iInvitees.Add(New Invitee(CType(iInvite.classId, ClassType), CType(iInvite.genderId, GenderType), iInvite.invitee, iInvite.id, iInvite.moderator, CType(iInvite.raceId, RaceType), iInvite.status))
            Next
        End Sub

        ''' <summary>
        ''' A class that defines an invitee
        ''' </summary>
        ''' <remarks></remarks>
        Public Class Invitee
            Private cClass As ClassType
            Private gGender As GenderType
            Private strInvitee As String
            Private intInviteeID As Integer
            Private intLevel As Integer
            Private blnModerator As Boolean
            Private rRace As RaceType
            Private strStatus As String

            ''' <summary>
            ''' The invitee's class
            ''' </summary>
            ''' <value></value>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public ReadOnly Property [Class]() As ClassType
                Get
                    Return cClass
                End Get
            End Property

            ''' <summary>
            ''' The invitee's gender
            ''' </summary>
            ''' <value></value>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public ReadOnly Property Gender() As GenderType
                Get
                    Return gGender
                End Get
            End Property

            ''' <summary>
            ''' The name of the invitee
            ''' </summary>
            ''' <value></value>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public ReadOnly Property Invitee() As String
                Get
                    Return strInvitee
                End Get
            End Property

            ''' <summary>
            ''' The identification number of the invitation
            ''' </summary>
            ''' <value></value>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public ReadOnly Property InviteeID() As Integer
                Get
                    Return intInviteeID
                End Get
            End Property

            ''' <summary>
            ''' Determines if the invitee is an event moderator
            ''' </summary>
            ''' <value></value>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public ReadOnly Property Moderator() As Boolean
                Get
                    Return blnModerator
                End Get
            End Property

            ''' <summary>
            ''' The race of the invitee
            ''' </summary>
            ''' <value></value>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public ReadOnly Property Race() As RaceType
                Get
                    Return rRace
                End Get
            End Property

            ''' <summary>
            ''' The status of the invitation
            ''' </summary>
            ''' <value></value>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public ReadOnly Property Status() As String
                Get
                    Return strStatus
                End Get
            End Property

            ''' <summary>
            ''' A constructor to create new invitee information
            ''' </summary>
            ''' <param name="cClass"></param>
            ''' <param name="gGender"></param>
            ''' <param name="strInvitee"></param>
            ''' <param name="intInviteeID"></param>
            ''' <param name="blnModerator"></param>
            ''' <param name="rRace"></param>
            ''' <param name="strStatus"></param>
            ''' <remarks></remarks>
            Public Sub New(ByVal cClass As ClassType, ByVal gGender As GenderType, ByVal strInvitee As String, ByVal intInviteeID As Integer, ByVal blnModerator As Boolean, ByVal rRace As RaceType, ByVal strStatus As String)
                Me.cClass = cClass
                Me.gGender = gGender
                Me.strInvitee = strInvitee
                Me.intInviteeID = intInviteeID
                Me.blnModerator = blnModerator
                Me.rRace = rRace
                Me.strStatus = strStatus
            End Sub

        End Class

    End Class

End Namespace